from django.contrib import admin
from django.utils.safestring import mark_safe

from . import models


@admin.register(models.Category)
class CategoryAdmin(admin.ModelAdmin):

    list_display = ['name', 'slug']
    prepopulated_fields = {'slug': ('name',)}


@admin.register(models.Product)
class ProductAdmin(admin.ModelAdmin):

    list_display = ['name', 'slug', 'price', 'get_html_photo', 'available', 'foo']
    list_filter = ['available', 'created', 'updated']
    list_editable = ['price', 'available']
    prepopulated_fields = {'slug': ('name',)}
    readonly_fields = ('created', 'updated', 'get_html_photo')
    save_on_top = True

    def get_html_photo(self, object):

        """ Заменяем путь изображния на само изображение """

        if object.image:
            return mark_safe(f'<img src="{object.image.url}" width=70px>')
        else: return '-'

    def foo(self, object):

        return 'foo'

    get_html_photo.short_description = 'Thumbnail'
    foo.short_description = 'FOO'
